﻿#region

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class EditProfile : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var staff = StaffBLL.GetStaffById(Convert.ToInt32(User.Identity.Name));

                tbFName.Text = staff.FirstName;
                tbLName.Text = staff.LastName;
                tbEMail.Text = staff.EMail;
                tbTelephone.Text = staff.TelephoneNo;
                tbAddress.Text = staff.Address;
            }
        }

        protected void tbEMail_OnTextChanged(object sender, EventArgs e)
        {
            cvEMail.Validate();
        }

        protected void cvEMail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            var staff = StaffBLL.GetStaffById(Convert.ToInt32(User.Identity.Name));
            args.IsValid = tbEMail.Text == staff.EMail || !StaffBLL.IsEMailTaken(tbEMail.Text);
        }

        protected void btnReset_OnClick(object sender, EventArgs e)
        {
            Server.Transfer("EditProfile.aspx");
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var staffId = Convert.ToInt32(User.Identity.Name);
                StaffBLL.EditStaff(staffId, tbFName.Text, tbLName.Text, tbEMail.Text, tbTelephone.Text, tbAddress.Text,
                    "Administrator", null);
                Response.Redirect("Profile.aspx");
            }
        }
    }
}